/// @tags:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define rint register int

using std::cin;
using std::cout;

namespace BlueQuantum {

typedef long long LL;

constexpr int maxn = 5e3 + 3;

int a[maxn];
// std::priority_queue<int> global;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

inline LL solve(int ncnt, int &ptr) {
  // static std::priority_queue<int> tmp;
  // for (int i = 1; i <= ncnt; ++i) {
  // tmp.push(global.top());
  // global.pop();
  // }
  if (ncnt == 2) {
    // int maxx = tmp.top();
    // tmp.pop();
    ptr -= 2;
    return (LL)a[ptr + 1] * a[ptr + 2] * 2;
  }
  LL res = 0;
  int maxx = a[ptr--];
  int l = a[ptr--];
  int r = a[ptr--];
  res += (LL)maxx * (l + r);
  ncnt -= 3;
  while (ncnt) {
    res += (LL)l * a[ptr];
    l = a[ptr--];
    ncnt--;
    if (!ncnt) break;
    res += (LL)r * a[ptr];
    r = a[ptr--];
    ncnt--;
  }
  res += (LL)l * r;
  return res;
}

inline int main() {
  int n, m;
  cin >> n;
  for (rint i = 1; i <= n; ++i) {
    cin >> a[i];
    // global.push(a[i]);
  }
  std::sort(a + 1, a + n + 1);
  cin >> m;
  for (rint i = 1; i <= m; ++i) {
    int k, ccnt = 0, ncnt = 0;
    LL ans = 0;
    cin >> k;
    if (k != 0) {
      ccnt = gcd(n, k);
      ncnt = n / ccnt;
      int ptr = n;
      for (rint j = 1; j <= ccnt; ++j) ans += solve(ncnt, ptr);
      // for (rint j = 1; j <= n; ++j) global.push(a[j]);
    } else {
      for (rint i = 1; i <= n; ++i) ans += (LL)a[i] * a[i];
    }
    cout << ans << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("algebraic.in", "r", stdin);
  freopen("algebraic.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
